<?php
class panel_inbound_adm extends Controller {
	     function __construct()
	     {
	     	parent::controller();
	     	 $this->load->model(array('mservicios','mprototipotransacionentrada','usuarios','mmodeloservicios','mtransaccionentrada','detallerentradas','mtipoentrada','mregistro','mmotivosalida'));
	     }
	
          function index() 
           {
            $records = $this->mmotivosalida->LoadEstatus();
            $use = $this->usuarios->LoadUsuariosDepar(3);
           	
           	 $this->load->view('main',array('content_page' => 'graficos',
           	                                'servicio'=> $this->tipoCompania(1),
           	                                'servicio2'=> $this->tipoCompania(2),
           	                                'servicio3'=> $this->tipoCompania(3),
           	                                'servicio4'=> $this->tipoCompania(4),
           	                                'servicio5'=> $this->tipoCompania(5),
           	                                'imagen2'=>$this->tipoSalida(),
           	                                'entrada2'=>$this->tipoEntrada(1),
           	                                'entrada'=>$this->tipoEntrada(2),
           	                                'entrada3'=>$this->tipoEntrada(3),
           	                                'colores'=>$this->transacionPoRColor2(1,0),
           	                                'colores2'=>$this->transacionPoRColor2(2,0),
           	                                'salida'=>$records,
           	                                'use'=>$use,
           	                                'neg_Use'=>$this->tipoUseNegosio(),
           	                                'menutop'=>'4')); 
           	
           }
          
           function porUse()
           {
           	if (!$this->input->post('use')) {
           		$use = 9;
           	}
           	else 
           	$use = intval($this->input->post('use'));
            $use2 = $this->usuarios->LoadUsuariosDepar(3);
        
           	 $this->load->view('main',array('content_page' => 'graficos2',
           	                                'servicio'=> $this->tipoCompania2($use),          	                            
           	                                'imagen2'=>$this->tipoSalida2($use),
           	                                'entrada2'=>$this->tipoEntrada2($use),          	                            
           	                                'colores'=>$this->transacionPoRColor2(0,$use), 
           	                                'use'=>$use2,        	                            
           	                                'menutop'=>'4')); 
           }
           
            	function detruir()
           	{
           		$this->session->unset_userdata('fechaInicio');
           		$this->session->unset_userdata('fechaFin');
           		redirect('panel_inbound_adm');
           	}
           
           function fecha()
           {
           	if (!$this->input->post('solicitud')) {
           		return false;
           	}
           	
          
           	
           	$fecha1 = explode('/',$this->input->post('solicitud'));
           	$fecha2 = explode('/',$this->input->post('conctato'));
           	$ffecha1 = $fecha1[2].'-'.$fecha1[0].'-'.$fecha1[1].' 00:00:00';
           	$ffecha2 = $fecha2[2].'-'.$fecha2[0].'-'.$fecha2[1].' 00:00:00';
           	
           	$array = array(
           	                'fechaInicio'=>$ffecha2,
           	                'fechaFin'=>$ffecha1
           	               );
           	
           	$this->session->set_userdata($array);
           	redirect('panel_inbound_adm');
           }
           
           function encodeDataURL($strDataURL, $addNoCacheStr=false) {
            if ($addNoCacheStr==true) {
		    if (strpos(strDataURL,"?")<>0)
			  $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
		     else
			   $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
             }

	        return urlencode($strDataURL);
	
             }

    function datePart($mask, $dateTimeStr) {
             @list($datePt, $timePt) = explode(" ", $dateTimeStr);
            $arDatePt = explode("-", $datePt);
            $dataStr = "";
 
    if (count($arDatePt) == 1) {
        list($year, $month, $day) = $arDatePt;
        
        switch ($mask) {
        case "m": return $month;
        case "d": return $day;
        case "y": return $year;
        }
      
        return (trim($month . "/" . $day . "/" . $year));
    }
    return $dataStr;
}

function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) {

	if ($strXML=="")
        $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
    else
        $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";


    $chartIdDiv = $chartId . "Div";
    $ndebugMode = $this->boolToNum($debugMode);
    $nregisterWithJS = $this->boolToNum($registerWithJS);

$render_chart = <<<RENDERCHART

	<!-- START Script Block for Chart $chartId -->
	<div id="$chartIdDiv" align="center">
		Chart.
	</div>
	<script type="text/javascript">	
		//Instantiate the Chart	
		var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
		$tempData
		//Finally, render the chart.
		chart_$chartId.render("$chartIdDiv");
	</script>	
	<!-- END Script Block for Chart $chartId -->
RENDERCHART;

  return $render_chart;
}


function renderChartHTML($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode) {
  
    $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $this->boolToNum($debugMode);
    if ($strXML=="")
     
        $strFlashVars .= "&dataURL=" . $strURL;
    else
     
        $strFlashVars .= "&dataXML=" . $strXML;

$HTML_chart = <<<HTMLCHART
	<!-- START Code Block for Chart $chartId -->
	<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="$chartWidth" height="$chartHeight" id="$chartId">
		<param name="allowScriptAccess" value="always" />
		<param name="movie" value="$chartSWF"/>		
		<param name="FlashVars" value="$strFlashVars" />
		<param name="quality" value="high" />
		<embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
	</object>
	<!-- END Code Block for Chart $chartId -->
HTMLCHART;

  return $HTML_chart;
}


function boolToNum($bVal) {
    return (($bVal==true) ? 1 : 0);
}



function tipoCompania($var)
{
     
	 $records = $this->mservicios->Loadservicios();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo de Servicio' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
				 if ($var == 1) {
				 	 $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad($jur->getIdServicio())."' />";
				 }
				 elseif ($var == 2)
				 {
				 	 $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad2($jur->getIdServicio())."' />";
				 }
				 elseif ($var == 4)
				 {
				 	 $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad4($jur->getIdServicio())."' />";
				 }
				 elseif ($var == 5)
				  {
				 	 $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad5($jur->getIdServicio(),0)."' />";
				 }
				else 
                  $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad3($jur->getIdServicio())."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}

function tipoEntrada($var)
{

	 $records = $this->mtipoentrada->LoadEstatus();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo de Entrada' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
			$cant =$this->mtransaccionentrada->LoadGraficoEntrada($jur->getIdEstatus(),$var); 	
            $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}



function tipoSalida()
{

	 $records = $this->mmotivosalida->LoadEstatus();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo Salida' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
			$cant =$this->mtransaccionentrada->LoadGraficoSalida($jur->getIdEstatus()); 	
            $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}

  function CalcularDias($fechapar,$fechaact)
  {
       
		$fechapar = substr($fechapar,0,10);
		
		$fechaact = substr($fechaact,0,10);
		
		$fechapar_arr=explode('-',$fechapar);
		
	
		$fechapar_mk=mktime(0,0,0,$fechapar_arr[1],$fechapar_arr[2],$fechapar_arr[0]);
		$fechapar_mk=intval($fechapar_mk);

		$fechaact_arr=explode('-',$fechaact);
		$fechaact_mk=mktime(0,0,0,$fechaact_arr[1],$fechaact_arr[2],$fechaact_arr[0]);
		$fechaact_mk=intval($fechaact_mk);

		$segundos_diferencia =$fechaact_mk-$fechapar_mk;
		$dias_diferencia = $segundos_diferencia / (60 * 60 * 24);
		return  $dias_diferencia;

  }

   





  
 function transacionPoRColor2($var,$use)
 {
	$transaccion = $this->mtransaccionentrada->LoadTransacion2($var,$use);
	//$colores_array_etapa=$this->mregistro->loadModeloEtapas();
	$verde = 0;
	$amarillo = 0;
	$rojo = 0;
	
	$colores_array_etapa=$this->mregistro->loadModeloEtapas();
    $colores_etapa=$colores_array_etapa[0];
  if ($transaccion > 0) {
  	
 
	foreach ($transaccion as $entrada)
	{
	
		 if ($var == 1) {
		 	$cant = $this->CalcularDias($entrada->getDateSolicitud(),$entrada->getProximoContacto());
		 }
		 else 
		 {
		 	$cant = $this->CalcularDias($entrada->getDateSistema(),$entrada->getProximoContacto());
		 }
		
		
		
		$cant2 = $this->CalcularDias(date("Y-m-d"),$entrada->getProximoContacto());
		
		$porcien = $cant2 * 100;
		@$porcien = intval($porcien) / intval($cant);
		$porcien = 100 - intval($porcien);
		
		if ($porcien < $colores_etapa->getAmarilloModelo()) {
		    $verde++;
		}
		elseif($porcien >= $colores_etapa->getAmarilloModelo() && $porcien < $colores_etapa->getRojoModelo()){
		     $amarillo++;
			}
		else 
		{
			$rojo++;
		}
		
		$porcien = 0;	
	   
	
 }
  }
 
 	$strXML = "";
    $strXML = "<chart caption = 'Por Color' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
 
 $strXML .= "<set label = 'Rojo' value ='".$rojo."' />";
 $strXML .= "<set label = 'Amarillo' value ='".$amarillo."' />";
 $strXML .= "<set label = 'Verde' value ='".$verde."' />";
 
 $strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);  
 
 
 }

 function tipoCompania2($var)
{
     
	 $records = $this->mservicios->Loadservicios();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo de Servicio' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
				 
				 	 $strXML .= "<set label = '".$jur->getNombreServicio()."' value ='".$this->mtransaccionentrada->Loadcantidad5($jur->getIdServicio(),$var)."' />";
				
				
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}


 
 function tipoUseNegosio()
{
     
	
	$usuario = $this->usuarios->LoadUsuarioByIdDep(2);


	 
	$strXML = "";
    $strXML = "<chart caption = 'Inbound por Usuario' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='' >";

    
    
     if($usuario != false) {
			foreach($usuario as $jur) 
			{	
			
			$strXML .= "<set label = '".$jur->getNombreUsuario()."' value ='".$this->mtransaccionentrada->LoadUseTransacion($jur->getIdUsuario())."' />";
				
				
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}


function tipoSalida2($var)
{

	 $records = $this->mmotivosalida->LoadEstatus();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo Salida' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
			$cant =$this->mtransaccionentrada->LoadGraficoSalida2($jur->getIdEstatus(),$var); 	
            $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}


function tipoEntrada2($var)
{

	 $records = $this->mtipoentrada->LoadEstatus();
	   
	 
	$strXML = "";
    $strXML = "<chart caption = 'Tipo de Entrada' bgColor='#ffffff' baseFontSize='11' showValues='1' xAxisName='1' >";
	
     if($records != false) {
			foreach($records as $jur) 
			{	
			$cant =$this->mtransaccionentrada->LoadGraficoEntrada2($jur->getIdEstatus(),$var); 	
            $strXML .= "<set label = '".$jur->getNombre()."' value ='".$cant."' />";
			}
	}

$strXML .= "</chart>";

return $this->renderChartHTML(base_url()."public/swf_charts/Pie3D.swf", "",$strXML, "maestro", 365, 260, false);
}
 
}
?>